package com.example.Controller;

import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.nio.channels.MulticastChannel;
import java.util.List;

/**
 * Created by Administrator on 2017/5/11.
 */
@Controller

public class FileLoadContoller {
    @RequestMapping(value = "/upload", method = RequestMethod.GET)
    public String upload() {
        return "/fileupload";
    }

    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    @ResponseBody
    public String savefile(@RequestParam("file") MultipartFile file) {
            if (!file.isEmpty()) {
                try {
                    // 这里只是简单例子，文件直接输出到项目路径下。
                    // 实际项目中，文件需要输出到指定位置，需要在增加代码处理。
                    // 还有关于文件格式限制、文件大小限制，详见：中配置。
                    BufferedOutputStream out = new BufferedOutputStream(
                            new FileOutputStream(new File(file.getOriginalFilename())));
                    out.write(file.getBytes());
                    out.flush();
                    out.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                    return "上传失败," + e.getMessage();
                } catch (IOException e) {
                    e.printStackTrace();
                    return "上传失败," + e.getMessage();
                }
                return "上传成功";
            } else {
                return "上传失败，因为文件是空的.";
            }
        }
    @RequestMapping(value = "/filesload",method = RequestMethod.GET)
    public String filesload(){
        return "/files";
    }
    @RequestMapping(value = "/filesload",method = RequestMethod.POST)
    @ResponseBody
    public String filesloads(HttpServletRequest httpServletRequest){
        List<MultipartFile> files =  ((MultipartHttpServletRequest)httpServletRequest).getFiles("file");
        try {

        }catch (Exception e){
            e.printStackTrace();
        }
        return "上传成功";
    }
    }
